package com.pktechan.api.service.impl;

import com.pktechan.api.base.BaseMapper;
import com.pktechan.api.base.BaseServiceImpl;
import com.pktechan.api.dao.CloudTicketFlowMapper;
import com.pktechan.api.dao.CloudTicketFlowMapper;
import com.pktechan.api.entity.CloudTicket;
import com.pktechan.api.entity.CloudTicketFlow;
import com.pktechan.api.entity.CustBase;
import com.pktechan.api.enumeration.*;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.api.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.api.sendMiniMessage.bean.DistributorIntegralIntoMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.service.*;
import com.pktechan.api.vo.CloudTicketFlowVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service("cloudTicketFlowService")
@Transactional
public class CloudTicketFlowServiceImpl extends BaseServiceImpl<CloudTicketFlow> implements CloudTicketFlowService {
    private static final Logger logger = LoggerFactory.getLogger(CloudTicketFlowServiceImpl.class);

    @Autowired
    private CloudTicketFlowMapper cloudTicketFlowMapper;
    @Autowired
    private CloudTicketService cloudTicketService;
    @Autowired
    private CustBaseService custBaseService;
    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CloudTicketFlow> getBaseMapper() {
        return cloudTicketFlowMapper;
    }

    /**
     * 根据订单ID查询应得云酒票ID
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public List<CloudTicketFlow> getGenerateListByTradeOrderId(Integer tradeOrderId) {
        return cloudTicketFlowMapper.getGenerateListByTradeOrderId(tradeOrderId);
    }

    /**
     * 查看收入明细列表
     *
     * @param custId
     * @return
     */
    @Override
    public List<CloudTicketFlowVO> getIncomeDetailsListByCustId(Integer custId) {
        return cloudTicketFlowMapper.getIncomeDetailsListByCustId(custId);
    }

    /**
     * 查看兑付明细
     *
     * @param custId
     * @return
     */
    @Override
    public List<CloudTicketFlowVO> getCashingDetailsByCustId(Integer custId) {
        return cloudTicketFlowMapper.getCashingDetailsByCustId(custId);
    }

    /**
     * 转让兑付
     *
     * @param cloudTicketFlow
     */
    @Override
    public void transferableCashing(CloudTicketFlow cloudTicketFlow, CustBase custBase) {
        logger.info("查询当前登录人酒票账户状态");
        CloudTicket outCloudTicket = cloudTicketService.getByCustId(custBase.getId());
        if (YesOrNoEnum.NO.getCode().equals(outCloudTicket.getStatus())) {
            throw new CustomException("当前账户异常,不允许转出。");
        }
        logger.info("查询受让人信息");
        CustBase intoCustBase = custBaseService.getById(cloudTicketFlow.getTransferCustId());
        if (intoCustBase == null) {
            logger.info("受让人id=" + cloudTicketFlow.getTransferCustId() + "不存在-------");
            throw new CustomException("受让人不存在");
        }
        if (intoCustBase.getId().equals(custBase.getId())) {
            logger.debug("不可转给自己");
            throw new CustomException("不可转给自己");
        }
//        if (!VipTypeEnum.DISTRIBUTOR.getCode().equals(intoCustBase.getVipType())) {
//            logger.info("配送商id=" + cloudTicketFlow.getTransferCustId() + "不是配送商");
//            throw new CustomException("您输入的配送商不是配送商，请重新输入");
//        }
        logger.info("查询配送商持仓是否为null");
        CloudTicket intoCloudTicket = cloudTicketService.getByCustId(cloudTicketFlow.getTransferCustId());
        if (intoCloudTicket == null) {
            logger.info("创建配送商持仓");
            intoCloudTicket = new CloudTicket();
            intoCloudTicket.setAvailableBalance(BigDecimal.ZERO);
            intoCloudTicket.setCustId(cloudTicketFlow.getTransferCustId());
            intoCloudTicket.setStatus(CloudPointStatusEnum.VALID.getCode());
            cloudTicketService.save(intoCloudTicket);
        }
        if (YesOrNoEnum.NO.getCode().equals(intoCloudTicket.getStatus())) {
            throw new CustomException("对方账户异常,不允许转入。");
        }
        logger.info("转出人的");
        cloudTicketFlow.setBalanceAfter(outCloudTicket.getAvailableBalance().subtract(cloudTicketFlow.getAmount()));//转让前酒票-变动酒票=转让后酒票
        cloudTicketFlow.setBusinessType(CloudPointBusinessTypeEnum.TRANSFER_OUT.getCode());//转出
        //添加转让流水 修改酒票
        addCloudTicketFlowAndUpdateCloudTicket(cloudTicketFlow, outCloudTicket);
        //受让人ID
        cloudTicketFlow.setTransferCustId(custBase.getId());
        logger.info("转入受让人的");
        logger.info("添加转入受让人" + cloudTicketFlow.getTransferCustId() + "的酒票流水");
        cloudTicketFlow.setBalanceAfter(intoCloudTicket.getAvailableBalance().add(cloudTicketFlow.getAmount()));//转让前酒票+变动酒票=转让后酒票
        cloudTicketFlow.setBusinessType(CloudPointBusinessTypeEnum.TRANSFER_INTO.getCode());//转入
        //添加配送商流水 修改酒票
        addCloudTicketFlowAndUpdateCloudTicket(cloudTicketFlow, intoCloudTicket);
        //配送商接收-配送商-转让换货余额-酒票兑换物品-酒票通知
//        DistributorIntegralIntoMessage distributorIntegralIntoMessage = new DistributorIntegralIntoMessage();
//        distributorIntegralIntoMessage.setBalance(cloudTicketFlow.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
//        //转出人姓名
//        distributorIntegralIntoMessage.setOutName(custBase.getName());
//        //转入人openId
//        distributorIntegralIntoMessage.setOpenId(intoCustBase.getOpenId());
//        SendMiniMessageStrategy distributorIntegralIntoStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.DISTRIBUTOR_INTEGRAL_INTO);
//        distributorIntegralIntoStrategy.sendMiniMessageToActiveMq(distributorIntegralIntoMessage);
    }

    /**
     * 保存酒票流水信息and修改酒票信息
     *
     * @param cloudTicketFlow
     * @param cloudTicket
     */
    public void addCloudTicketFlowAndUpdateCloudTicket(CloudTicketFlow cloudTicketFlow, CloudTicket cloudTicket) {
        logger.info("保存酒票流水信息");
        cloudTicketFlow.setCloudTicketId(cloudTicket.getId());
        cloudTicketFlow.setCustId(cloudTicket.getCustId());
        cloudTicketFlow.setBalancePre(cloudTicket.getAvailableBalance());//转让前酒票
        cloudTicketFlow.setStatus(CloudPointFlowStatusEnum.EFFECTIVE.getCode());//有效
        cloudTicketFlowMapper.save(cloudTicketFlow);
        logger.info("修改酒票信息");
        CloudTicket changeInTo = new CloudTicket();
        changeInTo.setId(cloudTicket.getId());
        changeInTo.setAvailableBalance(cloudTicketFlow.getBalanceAfter());
        cloudTicketService.update(changeInTo);
    }
}
